<template>
  <a-modal
    :title="modalInfo.title"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="角色名称">
          <a-input v-decorator="['roleName', {rules: [{required: true}]}]" />
        </a-form-item>
        <a-form-item label="角色CODE">
          <a-input v-decorator="['roleCode', {rules: [{required: true}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>

    <s-tree
      :loadData="myLoadData"
      checkable
      v-model="checkedKeys"
      :autoExpandParent="autoExpandParent"
      :expanded-keys="expandedKeys"
      @expand="onExpand"
      @select="onSelect"
    />
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { Tree } from 'ant-design-vue'
import { getRoleResource } from '@/api/role'
// 表单字段
const fields = ['id', 'roleName', 'roleCode']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    modalInfo: {
      type: Object,
      default: () => null
    },
    treeData: {
      type: Array,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      // （受控）展开指定的树节点
      expandedKeys: [],
      // 是否自动展开父节点
      autoExpandParent: true,
      // 勾选
      checkedKeys: [],
      selectedKeys: [],
      myLoadData: parameter => {
        const tmpObj = { 'id': 1 }
        const requestParameters = Object.assign({}, parameter, tmpObj)
        console.log('myLoadData loadData request parameters:', requestParameters)
        return getRoleResource(requestParameters).then(res => {
          if (res.status === 200) {
            console.log('myLoadData res.result.dataList:', res.result.dataList)
            return res.result.dataList
            // this.treeData = res.result.dataList
            // // 通过路由进行反向赋值
            // this.$refs.roleModalForm.checkedKeys = res.result.checkedKeys
            // this.initCheckedKeyList = res.result.checkedKeys
            // console.info('treeData ==>', this.treeData)
          }
        })
      }
    }
  },
  components: {
    STree: Tree
  },
  created () {
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  watch: {
    checkedKeys (val) {
      console.log('onCheck', val)
    }
  },
  methods: {
    onExpand (expandedKeys) {
      console.log('onExpand', expandedKeys)
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onCheck (checkedKeys) {
      console.log('onCheck', checkedKeys)
      this.checkedKeys = checkedKeys
    },
    onSelect (selectedKeys, info) {
      console.log('onSelect', info)
      this.selectedKeys = selectedKeys
    }
  }
}
</script>
